<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class PSB extends CI_Controller {

    function __construct() { //inisialisasi library dan helper yang perlu di load
        parent::__construct();
        $this->load->helper('form');
        $this->load->library('parser');
        $this->load->model('all');
    }

    function index() {
        $data['blog_title'] = 'Pendaftaran Siswa Baru';
        if ($this->all->social()) {
            $data['social'] = $this->all->social();
        } else {
            $data['social'] = array();
        }
        $this->parser->parse('themes/head', $data);
        $this->load->view('themes/open_content');
        $this->load->view('themes/open_sidebar_left');
        $this->load->view('themes/sidebar_login');
        $this->load->view('themes/close_sidebar_left');
        $this->load->view('themes/open_sidebar_right');
        $this->load->view('themes/psb', $data);
        $this->load->view('themes/close_sidebar_right');
        $this->load->view('themes/close_content');
        $this->load->view('themes/footer');
    }

    function daftar() {
        $this->load->library('form_validation');
        $config = array(
            array(
                'field' => 'nama',
                'label' => 'Username',
                'rules' => 'required|min_length[3]'
            ),
            array(
                'field' => 'sex',
                'label' => 'Jenis Kelamin',
                'rules' => 'required|max_leght[10]'
            ),
            array(
                'field' => 'tempat',
                'label' => 'Tempat Lahir',
                'rules' => 'required'
            ),
            array(
                'field' => 'tgl',
                'label' => 'Tanggal Lahir',
                'rules' => 'required'
            ),
            array(
                'field' => 'sekolah',
                'label' => 'Sekolah Asal',
                'rules' => 'required'
            ),
            array(
                'field' => 'kabupaten',
                'label' => 'Kabupaten Asal',
                'rules' => 'required'
            ),
            array(
                'field' => 'ortu',
                'label' => 'Nama Orang Tua',
                'rules' => 'required'
            ),
            array(
                'field' => 'nilai',
                'label' => 'Nilai',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->form_validation->run() == FALSE) {
            $data['error'] = validation_errors();
            $data['input'] = array(
                'nama' => $this->input->post('nama'),
                'jenis_kelamin' => $this->input->post('sex'),
                'tmpt_lahir' => $this->input->post('tempat'),
                'tgl_lahir' => $this->input->post('tgl'),
                'asal_sekolah' => $this->input->post('sekolah'),
                'kabupaten_asal' => $this->input->post('kabupaten'),
                'nama_orangtua' => $this->input->post('ortu'),
                'nilai' => $this->input->post('nilai')
            );
            $this->daftar_error($data);
        } else {
            $input = array(
                'nama' => $this->input->post('nama'),
                'jenis_kelamin' => $this->input->post('sex'),
                'tmpt_lahir' => $this->input->post('tempat'),
                'tgl_lahir' => $this->input->post('tgl'),
                'asal_sekolah' => $this->input->post('sekolah'),
                'kabupaten_asal' => $this->input->post('kabupaten'),
                'nama_orangtua' => $this->input->post('ortu'),
                'nilai' => $this->input->post('nilai'),
                'tahun_ajaran' => date('Y')
            );
            $this->all->daftar_psb($input);
            redirect('psb/success');
        }
    }

    function daftar_error($data) {
        $data['blog_title'] = 'Pendaftaran Siswa Baru';
        if ($this->all->social()) {
            $data['social'] = $this->all->social();
        } else {
            $data['social'] = array();
        }
        $this->parser->parse('themes/head', $data);
        $this->load->view('themes/open_content');
        $this->load->view('themes/open_sidebar_left');
        $this->load->view('themes/sidebar_login');
        $this->load->view('themes/close_sidebar_left');
        $this->load->view('themes/open_sidebar_right');
        $this->load->view('themes/daftar', $data);
        $this->load->view('themes/close_sidebar_right');
        $this->load->view('themes/close_content');
        $this->load->view('themes/footer');
    }

    function success() {
        $data['blog_title'] = 'Pendaftaran Success';
        if ($this->all->social()) {
            $data['social'] = $this->all->social();
        } else {
            $data['social'] = array();
        }
        $this->parser->parse('themes/head', $data);
        $this->load->view('themes/open_content');
        $this->load->view('themes/open_sidebar_left');
        $this->load->view('themes/sidebar_login');
        $this->load->view('themes/close_sidebar_left');
        $this->load->view('themes/open_sidebar_right');
        $this->load->view('themes/success');
        $this->load->view('themes/close_sidebar_right');
        $this->load->view('themes/close_content');
        $this->load->view('themes/footer');
    }

}

?>